/*
Auhtor       : Pratibh Prakash
Company      : Fujitsu America
Created Date : 22 Sep-2009
Description  : Test class for Trigger: updateTerritoryNameField

Modified     : Andrey Volosevich
Modified Date: 11 Dec-2009
Description	 : Updated unit tests to match the updated version of the trigger
*/
@isTest
private class updateTerritoryNameFieldTest {
    static testMethod void myUnitTest() {
    	List<sundog_deprm2__Partner_Territory__c> sdTerritoryforInsert = new List<sundog_deprm2__Partner_Territory__c>(); 
		List<sundog_deprm2__Postal_Code__c	> sdPostalCodeforInsert = new List<sundog_deprm2__Postal_Code__c>();
		
		//Preparing mock Data
		Id curUser = UserInfo.getUserId();
		sundog_deprm2__Partner_Territory__c territory1 = new sundog_deprm2__Partner_Territory__c(Name = 'Territory 1', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory1);
		sundog_deprm2__Partner_Territory__c territory2 = new sundog_deprm2__Partner_Territory__c(Name = 'Territory 2', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory2);
		sundog_deprm2__Partner_Territory__c territory3 = new sundog_deprm2__Partner_Territory__c(Name = 'Territory 2', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory3);
		
		sundog_deprm2__Partner_Territory__c territory_longName1= new sundog_deprm2__Partner_Territory__c(Name = 'Territory11111111111111111111111111111111111111111111111111111111111111111111111', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory_longName1);
		sundog_deprm2__Partner_Territory__c territory_longName2= new sundog_deprm2__Partner_Territory__c(Name = 'Territory22222222222222222222222222222222222222222222222222222222222222222222222', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory_longName2);
		sundog_deprm2__Partner_Territory__c territory_longName3= new sundog_deprm2__Partner_Territory__c(Name = 'Territory33333333333333333333333333333333333333333333333333333333333333333333333', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory_longName3);
		sundog_deprm2__Partner_Territory__c territory_longName4= new sundog_deprm2__Partner_Territory__c(Name = 'Territory44444444444444444444444444444444444444444444444444444444444444444444444', sundog_deprm2__User__c= curUser);
		sdTerritoryforInsert.add(territory_longName4);
		insert sdTerritoryforInsert;
		
		sundog_deprm2__Postal_Code__c	 postalCode1 = new sundog_deprm2__Postal_Code__c	(Name='110010', sundog_deprm2__Country__c = 'USA', sundog_deprm2__Partner_Territory__c = territory1.Id, sundog_deprm2__Lead_Type__c='Digital');
		sdPostalCodeforInsert.add(postalCode1);
		sundog_deprm2__Postal_Code__c	 postalCode2 = new sundog_deprm2__Postal_Code__c	(Name='110010', sundog_deprm2__Country__c = 'USA', sundog_deprm2__Partner_Territory__c = territory2.Id, sundog_deprm2__Lead_Type__c='Physical');
		sdPostalCodeforInsert.add(postalCode2);
		sundog_deprm2__Postal_Code__c	 postalCode3 = new sundog_deprm2__Postal_Code__c	(Name='110010', sundog_deprm2__Country__c = 'Japan', sundog_deprm2__Partner_Territory__c = territory3.Id);
		sdPostalCodeforInsert.add(postalCode3);
		
		sundog_deprm2__Postal_Code__c postalCode_longName1 = new sundog_deprm2__Postal_Code__c	(Name='110011', sundog_deprm2__Country__c = 'USA', sundog_deprm2__Partner_Territory__c = territory_longName1.Id, sundog_deprm2__Lead_Type__c='Digital', sundog_deprm2__Lead_Sub_Type__c='ISR');
		sdPostalCodeforInsert.add(postalCode_longName1);
		sundog_deprm2__Postal_Code__c postalCode_longName2 = new sundog_deprm2__Postal_Code__c	(Name='110011', sundog_deprm2__Country__c = 'UK', sundog_deprm2__Partner_Territory__c = territory_longName2.Id, sundog_deprm2__Lead_Type__c='Digital', sundog_deprm2__Lead_Sub_Type__c='TSR');
		sdPostalCodeforInsert.add(postalCode_longName2);
		sundog_deprm2__Postal_Code__c postalCode_longName3 = new sundog_deprm2__Postal_Code__c	(Name='110011', sundog_deprm2__Country__c = 'USA', sundog_deprm2__Partner_Territory__c = territory_longName3.Id, sundog_deprm2__Lead_Type__c='Physical', sundog_deprm2__Lead_Sub_Type__c='ISR');
		sdPostalCodeforInsert.add(postalCode_longName3);
		sundog_deprm2__Postal_Code__c postalCode_longName4 = new sundog_deprm2__Postal_Code__c	(Name='110011', sundog_deprm2__Country__c = 'Japan', sundog_deprm2__Partner_Territory__c = territory_longName4.Id, sundog_deprm2__Lead_Type__c='Physical', sundog_deprm2__Lead_Sub_Type__c='TSR');
		sdPostalCodeforInsert.add(postalCode_longName4);
	
		insert sdPostalCodeforInsert;
		
		Test.startTest();
		
		Account testAccount = new Account(Name = 'Test Account', BillingPostalCode = '110010', BillingCountry = 'USA');
		insert testAccount;
		testAccount = [Select Id, Name, BillingPostalCode, BillingCountry, IM_Territories__c from Account where Id = :testAccount.Id];
		System.assertEquals('<' + territory1.Name + '>\n<' + territory2.Name + '>', testAccount.IM_Territories__c);
		
		testAccount.BillingPostalCode ='';
		update testAccount;
		testAccount = [Select Id, Name, BillingPostalCode, BillingCountry, IM_Territories__c from Account where Id = :testAccount.Id];
		System.assertEquals(null, testAccount.IM_Territories__c);
		
		testAccount.BillingPostalCode ='110011';
		testAccount.BillingCountry = 'Japan';
		update testAccount;
		testAccount = [Select Id, Name, BillingPostalCode, BillingCountry, IM_Territories__c from Account where Id = :testAccount.Id];
		System.assertEquals('<' + territory_longName4.Name + '>', testAccount.IM_Territories__c);
		
		testAccount.BillingCountry ='';
		update testAccount;
		testAccount = [Select Id, Name, BillingPostalCode, BillingCountry, IM_Territories__c from Account where Id = :testAccount.Id];
		System.assertEquals(null, testAccount.IM_Territories__c);
		
		
		Account testAccount1 = new Account(Name = 'Test Account');
		insert testAccount1;
		testAccount = [Select Id, Name, BillingPostalCode, BillingCountry, IM_Territories__c from Account where Id = :testAccount.Id];
		System.assertEquals(null, testAccount.IM_Territories__c);
		Test.stopTest();
		
		
    }
}